// pages/hospital-navigation/hospital-navigation.js
Page({
  data: {
    campusList: [],
    mapKey: ''
  },

  onLoad() {
    this.fetchMapKey();
    this.fetchCampusList();
  },

  fetchMapKey() {
    const that = this;
    wx.request({
      url: 'http://localhost:8005/hospital-nav/map-key',
      method: 'GET',
      success(res) {
        if (res.data && res.data.code === 200) {
          that.setData({ mapKey: res.data.data || '' });
        }
      }
    });
  },

  fetchCampusList() {
    const that = this;
    wx.request({
      url: 'http://localhost:8005/hospital-nav/list',
      method: 'GET',
      success(res) {
        if (res.data && res.data.code === 200) {
          const list = (res.data.data || []).map(item => ({
            name: item.hospitalArea,
            address: item.address,
            phone1: item.phone1,
            phone2: item.phone2,
            busRoute: item.busRoute,
            branchCode: item.branchCode,
            fullName: item.fullName,
            mapLat: item.mapLat,
            mapLng: item.mapLng,
            floorGuide: item.floorGuide
          }));
          that.setData({ campusList: list });
        } else {
          wx.showToast({ title: '加载医院导航失败', icon: 'none' });
        }
      },
      fail() {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
  },

  // 选择院区
  onCampusSelect(e) {
    const campus = e.currentTarget.dataset.campus;
    wx.navigateTo({
      url: `/pages/campus-detail/campus-detail?campus=${JSON.stringify(campus)}`
    });
  }
});
